package com.shopping.controller;

import com.shopping.pojo.Browse;
import com.shopping.pojo.Cart;
import com.shopping.pojo.Result;
import com.shopping.pojo.User;
import com.shopping.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.shopping.security.JwtUtil;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private UserService userService;

    //根据用户id获取用户名
    @GetMapping("/user/username")
    public Result getUsername(@RequestHeader("Authorization") String token){
        token = token.replace("Bearer ", "");
        Integer customerId = jwtUtil.extractCustomerId(token);
        log.info("查询用户名，顾客id为：{}", customerId);
        return Result.success(userService.getUserName(customerId));
    }
    //注销账号
    @DeleteMapping("/user")
    public Result deleteUser(@RequestHeader("Authorization") String token){
        token = token.replace("Bearer ", "");
        Integer customerId = jwtUtil.extractCustomerId(token);
        log.info("注销用户，顾客id为：{}", customerId);
        userService.deleteUser(customerId);
        return Result.success();
    }

    //管理员要使用到的接口也写在这了
    //获取用户列表
    @GetMapping("/adminhome/list")
    public Result list(){
        log.info("获取用户列表");
        List<User> userlist=userService.getUserList();
        return Result.success(userlist);
    }
    //获取用户浏览日志
    @GetMapping("/adminhome/browse/{id}")
    public Result browse(@PathVariable("id") Integer id){
        log.info("获取顾客浏览列表，顾客id为:{}",id);
        List<Browse> browselog=userService.getBrowseLog(id);
        return Result.success(browselog);
    }
    //获取用户购买日志
    @GetMapping("/adminhome/order/{id}")
    public Result order(@PathVariable("id") Integer id){
        log.info("获取顾客购买列表，顾客id为:{}",id);
        List<Cart> orderlog=userService.getOrderLog(id);
        return Result.success(orderlog);
    }
}
